import 'package:fluter_module/pages/order/order_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';

// 1. 创建一个自定义的Binding，继承和with的关系如下，里面什么都不用写
// 解决了哈. 还有其他的问题吗? 为啥原来那个flutter不行? 你之前如何切换的flutter分支?git checkout 3.22.0 应该是没有这个分支, 然后出现一些问题.
// 这里没有切换分支呀. 在cmd中切换的吗?是的
// 它这个看不了历史指令. . . :好吧,我之前用笔记本也是这个指令切换的,可行, 以后推荐用 git checkout -b 自定义的分支名称 目标的tag / branch
// 没事, 处理了就好哈.okok 我这边就先结束了哈. 行
class CustomFlutterBinding extends WidgetsFlutterBinding
    with BoostFlutterBinding {}

void main() {
  // 2. 这里的CustomFlutterBinding调用务必不可缺少，用于控制Boost状态的resume和pause
  CustomFlutterBinding();
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  // 3. 路由表
  Map<String, FlutterBoostRouteFactory> routerMap = {
    '/OrderDetail': (settings, isContainerPage, uniqueId) {
      return MaterialPageRoute(
        settings: settings,
        builder: (BuildContext ctx) {
          return const OrderPage();
        },
      );
    }
  };

  // 路由工厂函数
  Route<dynamic> routeFactory(
      RouteSettings settings, bool isContainerPage, String? uniqueId) {
    FlutterBoostRouteFactory? fn = routerMap[settings.name];
    if (fn == null) {
      throw FlutterError(
          'Route "${settings.toString()}" is not defined in routerMap.');
    }
    return fn(settings, isContainerPage, uniqueId)!;
  }

  @override
  Widget build(BuildContext context) {
    // flutter_boost 接管
    return FlutterBoostApp(
      routeFactory,
      // Flutter 侧直接预览需要，需要使用 Deveco Studio 导入 .ohos 项目进行自动签名预览至鸿蒙设备
      initialRoute: '/OrderDetail',
      appBuilder: (home) {
        return MaterialApp(
          builder: (context, child) => home,
        );
      },
    );
  }
}
